<?php

/*
*
*  This file is part of ValerieEngine
*  ValerieEngine is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  ValerieEngine is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with ValerieEngine.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
 * Implementation of ValerieEngine class
 * @author Dutchak Vitaly
 */
class VEngine {

    static public $register;
    static public $router;

    static private $_startTime;
    static private $_debugcount = 0;

    const WARNING = E_USER_WARNING;
    const NOTICE = E_USER_NOTICE;
    const ERROR = E_USER_ERROR;

    /**
     * Function which initialize ValerieEngine
     * @static
     * @return void
     */

    public final static function Init() {

        self::GetEngineStartTime();
        ErrorHandler::Init();

        if (!is_object(self::$register)) {
            self::$register = new Register();
        }

    }

    /**
     * Return time in seconds when Engine was started, if it's first call
     * set start time first
     * @static
     * @return float
     */
    public final static function GetEngineStartTime() {

        if (empty(self::$_startTime)) {
            self::$_startTime = microtime(true);
        }

        return floatval(self::$_startTime);
    }

    /**
     * Return time in seconds how much Engine is running
     * @static
     * @return float
     */
    public final static function GetEngineRunningTime() {

        return round (floatval(microtime(true) - self::GetEngineStartTime()),6);

    }

    /**
     * Show debug div with printed variable $var
     * @static
     * @param mixed $var var for debug
     * @return void
     */
    public final static function Debug($var) {

        if (!is_array($var) && !is_object($var)) {
            $var = str_replace(array("<", ">"), array("&lt;", "&gt;"), $var);
        }
        self::$_debugcount++;

        $out = '<pre style="border: 1px dotted black; background-color: #EEEECC; font-family: Verdana; font-size: 12px; text-align: left; color: #000000; padding: 10px;" ondblclick="var dwin = document.getElementById(\'debugwindow_' . self::$_debugcount . '_long\');if (dwin.style.display == \'none\')document.getElementById(\'debugwindow_' . self::$_debugcount . '_long\').style.display = \'block\';else document.getElementById(\'debugwindow_' . self::$_debugcount . '_long\').style.display = \'none\';" > DEBUG (' . self::$_debugcount . ')<div id="debugwindow_' . self::$_debugcount . '_long" style="display: none;">';
        $out .= '<br />' . print_r($var, true) . '<br /></div></pre>';

        print $out;
    }

    /**
     * Trigger new error
     * @static
     * @param string $msg - Error message
     * @return void
     */
    public final static function Error($msg) {

        trigger_error($msg, self::ERROR);

    }

    /**
     * Trigger new warning
     * @static
     * @param string $msg - Warning message
     * @return void
     */
    public final static function Warning($msg) {

        trigger_error($msg, self::WARNING);

    }

    /**
     * Trigger new notice
     * @static
     * @param string $msg - Notice message
     * @return void
     */
    public final static function Notice($msg) {

        trigger_error($msg, self::NOTICE);

    }

    /**
     * Return memory usage by PHP in MBytes
     * @static
     * @return float
     */
    public final static function GetMemoryUsage(){

        return round((memory_get_usage(true) / 1024 / 1024), 2);

    }

    /**
     * Return memory peak usage by PHP in MBytes
     * @static
     * @return float
     */
    public final static function GetMemoryPeakUsage(){

        return round((memory_get_peak_usage(true) / 1024 / 1024),2);

    }

}

?>
